import { isEmpty } from "cl-admin/utils";
import { mapState } from "vuex";

export default {
	computed: {
		...mapState('common', {
			brandList: 'brands'
		}),
		...mapState('biz', ['bizAll'])
	},
	methods: {
		toggleClass(arr = [], elem, key = 'id') {
			return arr.some(item => item[key] == elem[key]);
		},
		toChecked(arr, elem, key = 'id') {
			let isIncludes = this.toggleClass(arr, elem, key);
			!isIncludes ? arr.push(elem) : this.removeEle(arr, elem, key);
		},
		removeEle(arr, elem, key = 'id') {
			let includesIndex;
			arr.map((item, index) => {
				if (item[key] == elem[key]) {
					includesIndex = index
				}
			});
			arr.splice(includesIndex, 1);
		},
		setApproverStr(nodeConfig) {
			if (nodeConfig.settype == 1) {
				if (nodeConfig.nodeUserList.length == 1) {
					return nodeConfig.nodeUserList[0].user_name;
				}
				if (nodeConfig.nodeUserList.length > 1) {
					if (nodeConfig.examineMode == 1) {
						return this.arrToStr(nodeConfig.nodeUserList);
					}
					if (nodeConfig.examineMode == 2) {
						return nodeConfig.nodeUserList.length + "人会签";
					}
				}

			}
			if (nodeConfig.settype == 2) {
				let level =
					nodeConfig.directorLevel == 1
						? "直接主管"
						: "第" + nodeConfig.directorLevel + "级主管";
				if (nodeConfig.examineMode == 1) {
					return level;
				} else if (nodeConfig.examineMode == 2) {
					return level + "会签";
				}
			}
			if (nodeConfig.settype == 4) {
				if (nodeConfig.selectRange == 1) {
					return "发起人自选";
				} else {
					if (nodeConfig.nodeUserList.length > 0) {
						if (nodeConfig.selectRange == 2) {
							return "发起人自选";
						} else {
							return "发起人从" + nodeConfig.nodeUserList[0].user_name + "中自选";
						}
					} else {
						return "";
					}
				}
			}
			if (nodeConfig.settype == 5) {
				return "发起人自己";
			}
			if (nodeConfig.settype == 7) {
				return (
					"从直接主管到通讯录中级别最高的第" +
					nodeConfig.examineEndDirectorLevel +
					"个层级主管"
				);
			}
			return ''
		},
		conditionHasErr(nodeConfig, index) {
			let empty = this.conditionStr(nodeConfig, index) == "请设置条件" &&
				index != nodeConfig.conditionNodes.length - 1
			return empty
		},
		conditionStr(nodeConfig, index) {
			let optTypeList = ["", "<", ">", "≤", "=", "≥"]
			let { conditionList, nodeUserList } = nodeConfig.conditionNodes[index];
			if (conditionList.length == 0) {
				return index == nodeConfig.conditionNodes.length - 1 &&
					nodeConfig.conditionNodes[0].conditionList.length != 0
					? "其他条件进入此流程"
					: "请设置条件";
			} else {
				let str = "";
				let arr = []
				for (let i = 0; i < conditionList.length; i++) {
					// let {
					// 	columnId,
					// 	columnType,
					// 	showType,
					// 	showName,
					// 	optType,
					// 	zdy1,
					// 	opt1,
					// 	zdy2,
					// 	opt2,
					// 	fixedDownBoxValue
					// } = conditionList[i];
					let item = conditionList[i]
					if (item.columnId == 0) {
						if (nodeUserList.length != 0) {
							str += "发起人属于：";
							str +=
								nodeUserList
									.map(v => v.user_name)
									.join("或") + " 并且 ";
						}
						arr.push("发起人属于：" + nodeUserList
							.map(v => v.user_name)
							.join("或"))
						continue
					}
					if (item.columnType == "BizRef") {
						if (isEmpty(item.zdy1)) continue;
						if (item.columnId == 2) {
							arr.push(`品牌属于：${this.bizRefStr(item.zdy1.map(Number), this.brandList?.map?.(v => ({
								label: v.bra_name,
								value: v.bra_id
							})))}`)
						}
						if (item.columnId == 3) {
							arr.push(`事业部属于：${this.bizRefStr(item.zdy1.map(Number), this.bizAll?.map?.(v => ({
								label: v.biz_name,
								value: v.biz_id
							})))}`)
						}

					}
					if (item.columnType == "String" && item.showType == "3") {
						if (!zdy1) continue
						arr.push(`${item.showName}属于：${this.dealStr(item.zdy1, JSON.parse(fixedDownBoxValue))}`)
					}
					if (item.columnType == "Double") {
						if (item.optType != 6 && item.zdy1) {
							let optTypeStr = optTypeList[item.optType];
							arr.push(`${item.showName} ${optTypeStr} ${item.zdy1}`)
						} else if (item.optType == 6 && item.zdy1 && item.zdy2) {
							arr.push(`${item.zdy1} ${optTypeList[item.opt1]} ${item.showName} ${optTypeList[item.opt2]} ${item.zdy2}`)
						}
						continue
					}
				}
				return arr.length > 0 ? arr.join(' 并且 ') : '请设置条件'
				// console.log(arr.length > 0 ? arr.join(' 并且 ') : '请设置条件');
				// return str ? str.substring(0, str.length - 4) : "请设置条件";
			}
		},
		arrToStr(arr) {
			if (!arr) return "";
			return arr
				.map(item => {
					return item.user_name;
				})
				.toString();
		},
		copyerStr(nodeConfig) {
			if (nodeConfig.nodeUserList.length != 0) {
				return this.arrToStr(nodeConfig.nodeUserList);
			} else {
				if (nodeConfig.ccSelfSelectFlag == 1) {
					return "发起人自选";
				}
			}
		},
		// 事业部关联数据
		bizRefStr(ids = [], list = []) {
			return list
				.filter(v => ids.includes(v.value))
				.map(v => v.label)
				.join(" 或 ")
		},
		dealStr(str, obj) {
			let arr = [];
			let list = str.split(",");
			for (var elem in obj) {
				list.map(item => {
					if (item == elem) {
						arr.push(obj[elem].value)
					}
				})
			}
			return arr.join("或")
		}
	}
}
